package mars.ns.visualisation

import mars.ns.HasState
import mars.ns.core.Network
import mars.ns.core.Link
import mars.ns.core.Node
import mars.ns.core.LinkType
import mars.ns.listeners.SimulationAdapter

class VisualisationListener[T <:Node](var net:Network[T]) extends SimulationAdapter[T]{
	var nvis : NetworkVisualizer[T] = null
    var first : Boolean = true
    var updated : Boolean = false

	def init {
		this.nvis = new PrefuseNetworkVisualizer[T](net)
		nvis.visualise
		this.first = false
	}
	
	override def update(net: Network[T], iterator: Int) {
		if(this.updated) {
			Thread.sleep(500)
			this.updated=false
		}
	}
	
	override def edgeAdded(from : T, to:T, lType :LinkType) {
	    // synchronized, else init can be executed multiple times
	    synchronized{
	    	if(this.first){init}
	    	this.updated = true
		    nvis.addEdge(from, to, lType)
		    nvis.markNode(from)
		    nvis.markNode(to)
	    }
	}
	
	override def nodeChanged(node :T){
	    synchronized{
	    	if(this.first){init }
	    	updated = true
	    	nvis.setNodeName(node,node.toString())
	    	nvis.markNode(node,new java.awt.Color(200,50,20))	  
	    }
	}
}